<?php

class GirlController extends Zend_Controller_Action {

    private static $VIETNAM = 'http://www.depvd.com/vn';
    private static $ASIA = 'http://www.depvd.com/asia';
    private static $US = 'http://www.depvd.com/us-uk';
    private static $NEW = 'http://www.depvd.com/new';
    private static $RANDOM = 'http://www.depvd.com/pajax/topic/list/random?';
    private static $CHANNEL_TEEN = 'http://www.depvd.com/channel/teen/vn';
    private static $CHANNEL_SEXY = 'http://www.depvd.com/channel/sexy';
    private static $TOP_20 = 'http://www.depvd.com/top20';

    public function checkAccessKeyParams() {
        $appid = $this->_request->getParam("key");
        $sig = $this->_request->getParam("sig");
        $debug = $this->_request->getParam("db");
        if ($debug == '1')
            return true;

        if ($appid == "" || $sig == "") {
            echo Sunny_ErrorHandled::handleError(Sunny_ErrorHandled::$INVAILD_PARAMS);
            return false;
        } else {

            if ($appid != Haibl_ConfigData::KEY && $appid != '256845' && $appid != '74523') {
                echo Sunny_ErrorHandled::handleError(Sunny_ErrorHandled::$NOT_FOUND_APIKEY);
                return false;
            } else {
                $params = $this->_request->getParams();
                $requestParams = '';
                foreach ($params as $key => $value) {
                    if ($key != 'controller' && $key != 'action' && $key != 'module' && $key != "sig") {
                        $requestParams.= $key . '=' . $value;
                    }
                }
                $requestParams.= Haibl_ConfigData::ACCESS_KEY;
                $encryptParams = md5($requestParams);

                if ($encryptParams == $sig) {
                    return true;
                } else {
                    echo Sunny_ErrorHandled::handleError(Sunny_ErrorHandled::$NOT_FOUND_APIKEY);
                    if (Haibl_ConfigData::DEBUG) {
                        echo "<p>requestParams = " . $requestParams . "</p>";
                        echo "<p>encryptParams = " . $encryptParams . "</p>";
                    }
                    return false;
                }
            }
        }
    }

    //http://localhost/public/girl/collectiondetail?id=1   
    public function collectiondetailAction() {
        $this->_helper->viewRenderer->setNoRender(true);
        if ($this->checkAccessKeyParams()) {
            $id = $this->_request->getParam("id") == "" ? 1 : $this->_request->getParam("id");

            $girl = new Model_Girl();
            $data = $girl->get_album_detail_by_albumid($id);
            
            $albums = $girl->get_album_by_id($id);
            if(!empty($albums)){
                $view = $albums['view'];
                $time = $albums['create_time'];
                $view = $view + 1;
                $girl->update_girl_album_viewer($id,$view,$time);
            }
            
            
            $returnData['errorId'] = Sunny_ErrorHandled::$DONE;
            $returnData['data'] = $data;

            echo json_encode($returnData);
        }
    }

//http://localhost/public/girl/collection?page=1&count=50
    public function collectionAction() {
        $this->_helper->viewRenderer->setNoRender(true);
        if ($this->checkAccessKeyParams()) {
            $page = $this->_request->getParam("page") == "" ? 1 : $this->_request->getParam("page");
            $count = $this->_request->getParam("count") == "" ? 1 : $this->_request->getParam("count");

            $girl = new Model_Girl();
            $data = $girl->get_album_by_page($page, $count);
            $returnData['errorId'] = Sunny_ErrorHandled::$DONE;
            $returnData['data'] = $data;

            echo json_encode($returnData);
        }
    }
    
    public function searchalbumAction() {
        $this->_helper->viewRenderer->setNoRender(true);
        if ($this->checkAccessKeyParams()) {
            $key = $this->_request->getParam("key") == "" ? 1 : $this->_request->getParam("key");
            $page = $this->_request->getParam("page") == "" ? 1 : $this->_request->getParam("page");
            $count = $this->_request->getParam("count") == "" ? 1 : $this->_request->getParam("count");

            $girl = new Model_Girl();
            $data = $girl->search_album_by_title($key, $page, $count);
            $returnData['errorId'] = Sunny_ErrorHandled::$DONE;
            $returnData['data'] = $data;

            echo json_encode($returnData);
        }
    }
    

    public function indexAction() {
        $this->_helper->viewRenderer->setNoRender(true);
        $appid = $this->_request->getParam("key");

        if ($this->checkAccessKeyParams()) {
            $method = $this->_request->getParam("method") == "" ? "new" : $this->_request->getParam("method");
            $page = $this->_request->getParam("page") == "" ? 1 : $this->_request->getParam("page");

            $baseUrl = '';
            switch ($method) {
                case 'teen' :
                    $baseUrl = self::$CHANNEL_TEEN;
                    break;
                case 'sexy' :
                    $baseUrl = self::$CHANNEL_SEXY;
                    break;
                case 'new' :
                    $baseUrl = self::$NEW;
                    break;
                case 'vn' :
                    $baseUrl = self::$VIETNAM;
                    break;
                case 'asia' :
                    $baseUrl = self::$ASIA;
                    break;
                case 'us' :
                    $baseUrl = self::$US;
                    break;
                case 'random' :
                    $baseUrl = self::$RANDOM;
                    break;
            }

            $lists = null;
            if ($method != 'random') {
                $baseUrl .= '/p' . $page;
                $lists = $this->getImageList($baseUrl);
                $returnData['errorId'] = Sunny_ErrorHandled::$DONE;
                $returnData['data'] = $lists;

                echo json_encode($returnData);
            } else {
                $rndk = $this->_request->getParam("rndk") == "" ? "" : $this->_request->getParam("rndk");
                $baseUrl.='rndk=' . $rndk;
                $lists = $this->getImageListRandom($baseUrl, $rndk);
                $returnData['errorId'] = Sunny_ErrorHandled::$DONE;
                $returnData['data'] = $lists;
                $returnData['rndk'] = $rndk;

                echo json_encode($returnData);
            }
        }
    }

    public function detailAction() {
        $appid = $this->_request->getParam("key");


        $this->_helper->viewRenderer->setNoRender(true);
        if ($this->checkAccessKeyParams()) {
            $method = $this->_request->getParam("method") == "" ? "new" : $this->_request->getParam("method");
            $page = $this->_request->getParam("page") == "" ? 1 : $this->_request->getParam("page");
            $url = $this->_request->getParam("url") == "" ? 1 : $this->_request->getParam("url");

            $lists = $this->getDetail($url);
            $returnData['errorId'] = Sunny_ErrorHandled::$DONE;
            $returnData['data'] = $lists;

            echo json_encode($returnData);
        }
    }

    private function getDetail($url) {
        $appid = $this->_request->getParam("key");


        $client = new Zend_Http_Client();
        $client->setUri($url);
        $response = $client->request();
        $html = $response->getBody();
        $dom = new Zend_Dom_Query($html);
        $results = $dom->query('.carousel-inner');

        $data = null;
        $dataIndex = 0;

        $results->rewind();
        $domDoc = $results->current();

        $img = $domDoc->getElementsByTagName('img');
        for ($index = 0; $index < $img->length; $index++) {
            $src = $img_child = $img->item($index)->getAttribute("data-original");
            $data[$index]['bigsrc'] = $img_child;
            $data[$index]['src'] = $img_child;

            $pos = strrpos($src, "_no.jpg");
            if ($pos == true) {
                $data[$index]['src'] = str_replace("_no.jpg", "_wi.jpg", $src);
                $data[$index]['thumbnailsrc'] = str_replace("_no.jpg", "_th.jpg", $src);
                $data[$index]['smallsrc'] = str_replace("_no.jpg", "_gr.jpg", $src);
            }
        }
        $dataIndex++;
        $results->next();

        return $data;
    }

    function filter($title) {
        $title = strtolower(trim($title));
        if (strpos($title, 'nude') !== false
                || strpos($title, 'can lộ lộ') !== false
                || strpos($title, 'quay') !== false
                || strpos($title, 'chim') !== false
                || strpos($title, 'trym') !== false
                || (strpos($title, 'sex') !== false && !strpos($title, 'sexy'))
                || strpos($title, 'quá chuẩn') !== false
                || strpos($title, 'không mặc áo') !== false
                || strpos($title, 'áo') !== false
                || strpos($title, 'khoe thân') !== false
                || strpos($title, 'cởi') !== false
                || strpos($title, 'cởi đồ') !== false
                || strpos($title, 'đồ') !== false
                || strpos($title, 'giường') !== false
                || strpos($title, 'bán') !== false
                || strpos($title, 'ngực trần') !== false
                || strpos($title, 'ngực') !== false
                || strpos($title, 'tuột') !== false
                || strpos($title, 'đầu ti') !== false
                || strpos($title, 'đầu ty') !== false
                || strpos($title, 'nhũ hoa') !== false
                || strpos($title, 'lọt khe') !== false
                || strpos($title, 'khỏa') !== false
                || strpos($title, 'khỏa thân') !== false
                || strpos($title, 'dâm') !== false
                || strpos($title, 'giường') !== false
                || strpos($title, 'bọt') !== false
                || strpos($title, 'tay') !== false
                || strpos($title, 'xà') !== false
                || strpos($title, 'phòng') !== false
                || strpos($title, 'rách') !== false
                || strpos($title, 'rửa') !== false
                || strpos($title, 'tắm') !== false
                || strpos($title, 'phan kim liên') !== false
                || strpos($title, 'gội') !== false) {
            return true;
        } else {
            return false;
        }
    }

    function getImageList($baseUrl) {
        $appid = $this->_request->getParam("key");


        $client = new Zend_Http_Client();
        $client->setUri($baseUrl);
        $response = $client->request();
        $html = $response->getBody();
        $dom = new Zend_Dom_Query($html);
        $results = $dom->query('.vd-topic-inner');

        $data = array();
        $dataIndex = 0;

        $results->rewind();
        while ($results->valid()) {
            $domDoc = $results->current();

            $span = $domDoc->getElementsByTagName('span');
            if (!empty($span)) {
                $title = $span->item(0)->nodeValue;
                if ($this->filter($title)) {
                    $results->next();
                    continue;
                }

                $data[$dataIndex]['title'] = $span->item(0)->nodeValue;
            }

            $a = $domDoc->getElementsByTagName('a');
            if (!empty($a)) {
                $a = $a->item(1);

                $data[$dataIndex]['img'] = $a->getAttribute("href");
                $a = $a->getElementsByTagName('img');
                if (!empty($a)) {
                    $a = $a->item(0);

                    $tmp = $a->getAttribute("data-original");
                    if (empty($tmp)) {
                        $tmp = $a->getAttribute("src");
                    }

                    $src = $data[$dataIndex]['src'] = $tmp;
                    $data[$dataIndex]['bigsrc'] = $tmp;

                    $pos = strrpos($src, "_wi.jpg");
                    if ($pos == true) {
                        $data[$dataIndex]['bigsrc'] = str_replace("_wi.jpg", "_no.jpg", $src);
                        $data[$dataIndex]['thumbnailsrc'] = str_replace("_wi.jpg", "_th.jpg", $src);
                        $data[$dataIndex]['smallsrc'] = str_replace("_wi.jpg", "_gr.jpg", $src);
                    }
                }
            }

            $li = $domDoc->getElementsByTagName('li');
            for ($index = 0; $index < $li->length; $index++) {
                $li_child = $li->item($index);
                $i = $li_child->getElementsByTagName('i');
                if (!empty($i)) {
                    for ($index_i = 0; $index_i < $i->length; $index_i++) {
                        $class_i = trim($i->item($index_i)->getAttribute("class"));
                        if ('icon-eye-open' == $class_i) {
                            $data[$dataIndex]['view'] = $li_child->nodeValue;
                        } else if ('icon-comment' == $class_i) {
                            $data[$dataIndex]['comment'] = $li_child->nodeValue;
                        }
                    }
                }
            }

            $div = $domDoc->getElementsByTagName('div');
            if (!empty($div)) {
                for ($index = 0; $index < $div->length; $index++) {
                    $child = $div->item($index);
                    $child_div = trim($child->getAttribute("class"));
                    if ('vd-topic-count' == $child_div) {
                        $i_count = $child->getElementsByTagName('i');
                        if (!empty($i_count)) {
                            $i = $i_count->item(3)->getAttribute("class");
                            if ('topic-type vd-icon-topic-album' == $i) {
                                $data[$dataIndex]['more'] = 1;
                            } else {
                                $data[$dataIndex]['more'] = 0;
                            }
                        }
                    }
                }
            }

            $dataIndex++;
            $results->next();
        }
        return $data;
    }

    public function top20Action() {
        $appid = $this->_request->getParam("key");


        $this->_helper->viewRenderer->setNoRender(true);
        if ($this->checkAccessKeyParams()) {

            $lists = $this->getTop20(self::$TOP_20);
            $returnData['errorId'] = Sunny_ErrorHandled::$DONE;
            $returnData['data'] = $lists;

            echo json_encode($returnData);
        }
    }

    function startsWith($haystack, $needle) {
        return !strncmp($haystack, $needle, strlen($needle));
    }

    private function getTop20($baseUrl) {
        $client = new Zend_Http_Client();
        $client->setUri($baseUrl);
        $response = $client->request();
        $html = $response->getBody();
        $dom = new Zend_Dom_Query($html);

        $results = $dom->query('.vd-media');

        $data = array();
        $dataIndex = 0;

        $results->rewind();

        while ($results->valid()) {

            $domDoc = $results->current();
            $a = $domDoc->getElementsByTagName('a');
            if (!empty($a)) {

                $href = $a->item(0)->getAttribute("href");
                if ($this->startsWith($href, 'http://www.depvd.com/model')) {
                    $data[$dataIndex]['href'] = $href;

                    $name = $a->item(1)->nodeValue;
                    $data[$dataIndex]['name'] = trim($name);

                    $img = $domDoc->getElementsByTagName('img');
                    if (!empty($img)) {
                        $thumbnail = $img->item(0)->getAttribute("src");
                        $data[$dataIndex]['thumbnail'] = $thumbnail;
                    }

                    $span = $domDoc->getElementsByTagName('span');
                    if (!empty($span)) {
                        $like = $span->item(2)->nodeValue;
                        $pos = strpos($like, 'like');
                        if ($pos != -1) {
                            $like = str_replace('like', '', $like);
                            $like = intval(trim($like));
                        }
                        $data[$dataIndex]['like'] = $like;
                    }
                }
            }

            $dataIndex++;
            $results->next();
        }

        return $data;
    }

    public function detailmodelAction() {
        $this->_helper->viewRenderer->setNoRender(true);
        if ($this->checkAccessKeyParams()) {
            $url = $this->_request->getParam("url") == "" ? 1 : $this->_request->getParam("url");

            $lists = $this->getDetailModel($url);
            $returnData['errorId'] = Sunny_ErrorHandled::$DONE;
            $returnData['data'] = $lists;

            echo json_encode($returnData);
        }
    }

    function getDetailModel($baseUrl) {
        $client = new Zend_Http_Client();
        $client->setUri($baseUrl);
        $response = $client->request();
        $html = $response->getBody();
        $dom = new Zend_Dom_Query($html);
        $results = $dom->query('.vd-xitin');

        $data = array();
        $dataIndex = 0;

        $results->rewind();

        while ($results->valid()) {
            $domDoc = $results->current();
            $title = $domDoc->getAttribute("title");
            if ($this->filter($title)) {
                $results->next();
                continue;
            }
            $data[$dataIndex]['title'] = $domDoc->getAttribute("title");

            $a = $domDoc->getElementsByTagName('a');
            if (!empty($a)) {
                $data[$dataIndex]['img'] = $a->item(0)->getAttribute("href");
            }

            $img = $domDoc->getElementsByTagName('img');
            if (!empty($img)) {
                $src = $img->item(0)->getAttribute("src");
                $data[$dataIndex]['src'] = $src;
                $data[$dataIndex]['bigsrc'] = $src;

                $pos = strrpos($src, "_gr.jpg");
                if ($pos == true) {
                    $data[$dataIndex]['src'] = str_replace("_gr.jpg", "_wi.jpg", $src);
                    $data[$dataIndex]['bigsrc'] = str_replace("_gr.jpg", "_no.jpg", $src);

                    $data[$dataIndex]['thumbnailsrc'] = str_replace("_gr.jpg", "_th.jpg", $src);
                    $data[$dataIndex]['smallsrc'] = str_replace("_gr.jpg", "_gr.jpg", $src);
                }
            }

            $i = $domDoc->getElementsByTagName('i');
            if (!empty($i)) {
                $class = $i->item(0)->getAttribute("class");
                if ($class == 'vd-topic-type vd-icon-topic-album') {
                    $data[$dataIndex]['more'] = 1;
                } else {
                    $data[$dataIndex]['more'] = 0;
                }
            }

            $dataIndex++;
            $results->next();
        }
        return $data;
    }

    function getImageListRandom($baseUrl, &$rndk) {
        $client = new Zend_Http_Client();
        $client->setUri($baseUrl);
        $response = $client->request();
        $html = $response->getBody();
        $dataJson = json_decode($html, true);

        $rndk = $dataJson['rndk'];

        $array = $dataJson['topics'];
        $index = 0;
        $data = array();
        if (!empty($array)) {
            foreach ($array as $key => $value) {
                $title = $value['title'];
                if ($this->filter($title)) {
                    continue;
                }
                $data[$index]['title'] = $value['title'];
                $data[$index]['more'] = !empty($value['typeClass']) ? 1 : 0;
                $data[$index]['img'] = 'http://www.depvd.com/' . $value['viewUrl'];
                $src = 'http://photo.depvd.com/' . $value['widgetImage'];
                $data[$index]['src'] = $src;
                $data[$index]['bigsrc'] = $src;

                $pos = strrpos($src, "_wi.jpg");
                if ($pos == true) {
                    $data[$index]['bigsrc'] = str_replace("_wi.jpg", "_no.jpg", $src);
                    $data[$index]['thumbnailsrc'] = str_replace("_wi.jpg", "_th.jpg", $src);
                    $data[$index]['smallsrc'] = str_replace("_wi.jpg", "_gr.jpg", $src);
                }

                $index++;
            }
        }
        return $data;
    }

}

?>